"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : security-overflow
Case Name   : create masking policy 创建脱敏策略时针对应用APP超长时是否会造成缓冲区溢出
Create At   : 2023/06/05
@zou_jialiang0501162244
Description :
    1. 创建测试用户
    2. 创建测试表，向测试表中插入数据
    3. 创建资源标签
    4. 创建针对应用APP超长的脱敏策略
Expect      :
    1. 成功
    2. 成功
    3. 成功
    4. 报错
History     :
"""

import os
import unittest

from yat.test import Node
from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant


class Overflow(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.cons = Constant()
        self.pri_node = Node('PrimaryDbUser')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.user = 'u_memory_0019'
        self.table = 't_memory_0019'

    def test_security(self):
        text1 = '-----step1:创建测试用户;expect:成功-----'
        self.log.info(text1)
        msg = self.pri_sh.execut_db_sql(
            f"drop user if exists {self.user} cascade;"
            f"create user {self.user} with password 'test@123';")
        self.log.info(msg)
        self.assertIn(self.cons.CREATE_ROLE_SUCCESS_MSG, msg, f"执行失败:{text1}")

        text2 = '-----step2:创建测试表并插入数据;expect:成功-----'
        self.log.info(text2)
        msg = self.pri_sh.execut_db_sql(
            f"drop table if exists {self.table} cascade;"
            f"create table {self.table}(id int primary key, "
            f"name varchar(20), creditcard varchar(20), "
            f"address varchar(50));"
            f"insert into {self.table} values("
            f"1, '张三', '1234-4567-7890-0123', 'Fuhua Street')")
        self.log.info(msg)
        self.assertIn(self.cons.CREATE_TABLE_SUCCESS, msg, f"执行失败:{text2}")
        self.assertIn(self.cons.INSERT_SUCCESS_MSG, msg, f"执行失败:{text2}")

        text3 = '-----step3:创建创建资源标签;expect:成功-----'
        self.log.info(text2)
        msg = self.pri_sh.execut_db_sql(
            f"create resource label person_label "
            f"add column ({self.table}.address);")
        self.log.info(msg)
        self.assertIn(self.cons.resource_label_create_success_msg, msg,
                      f"执行失败:{text3}")

        text4 = '-----step4:创建针对应用APP超长的脱敏策略;expect:报错-----'
        self.log.info(text4)
        app = 'a' * 16000
        sql = f"create masking policy mask_person_pol creditcardmasking " \
              f"on label (person_label) filter on roles ('{self.user}')," \
              f"IP('{self.pri_node.db_host}'),APP('{app}');"
        msg = self.pri_sh.execut_db_sql(sql)
        self.log.info(msg)
        self.assertTrue(msg.find('ERROR:') > -1, f"执行失败:{text4}")

    def tearDown(self):
        self.log.info('-----清理环境-----')
        msg = self.pri_sh.execut_db_sql(
            f"drop resource label person_label;"
            f"drop table if exists {self.table} cascade;"
            f"drop user {self.user} cascade;")
        self.log.info(msg)
        self.assertIn(self.cons.drop_resource_label_success, msg, f"删除标签失败")
        self.assertIn(self.cons.DROP_TABLE_SUCCESS, msg, f"删除表失败")
        self.assertIn(self.cons.DROP_ROLE_SUCCESS_MSG, msg, f"删除用户失败")
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
